#import <UIKit/UIKit.h>
#import "ColorFanViewController.h"
#define kCount 256
@implementation ColorFanViewController

@synthesize colorPicker;
@synthesize sampleView;
@synthesize redValueList;
@synthesize greenValueList;
@synthesize blueValueList;
@synthesize alphaSlider;
@synthesize alpha;

- (void)viewDidLoad {
    [super viewDidLoad];
    NSMutableArray * numberList = [[NSMutableArray alloc] initWithCapacity:kCount];
    for (int i=0; i<kCount; i++) {
        [numberList addObject:[NSNumber numberWithInt:i]];
    }
    redValueList = [[NSArray alloc] initWithArray:numberList];
    greenValueList = [[NSArray alloc] initWithArray:numberList];
    blueValueList = [[NSArray alloc] initWithArray:numberList];
    [numberList release];
    alpha = 1.0;
    [colorPicker selectRow:127 inComponent:0 animated:NO];
    [colorPicker selectRow:127 inComponent:1 animated:NO];
    [colorPicker selectRow:127 inComponent:2 animated:NO];
    [self setColor];
}

- (IBAction) buttonPressed {
    [self setColor];
}

-(IBAction) sliderMoved {
    alpha = alphaSlider.value;
    [self setColor];
}

-(void) setColor {
    NSInteger redRow = [colorPicker selectedRowInComponent:0];
    NSInteger greenRow = [colorPicker selectedRowInComponent:1];
    NSInteger blueRow = [colorPicker selectedRowInComponent:2];
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
    const CGFloat myColor[] = {redRow/255.0, greenRow/255.0, blueRow/255.0, alpha};
    sampleView.backgroundColor = [UIColor colorWithCGColor: CGColorCreate(rgb, myColor)];
    CGColorSpaceRelease(rgb);
}

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return 256;
}


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    switch (component) {
        case 0:
            return [NSString stringWithFormat:@"%@", [redValueList objectAtIndex:row]];
            break;
        case 1:
            return [NSString stringWithFormat:@"%@", [greenValueList objectAtIndex:row]];
            break;
        case 2:
            return [NSString stringWithFormat:@"%@", [blueValueList objectAtIndex:row]];
            break;
        default:
            break;
    }
    return nil;
}

- (void)dealloc {
    [colorPicker release];
    [redValueList release];
    [greenValueList release];
    [blueValueList release];
    [super dealloc];
}

@end
